package com.university.web;

import com.university.pojo.Book;
import com.university.pojo.Page;
import com.university.service.impl.BookService;
import com.university.service.impl.BookServiceImpl;
import com.university.utils.webUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;

/**
 * @author shk
 * @create 2021--05--08--14:00
 */
public class ClientBookServlet  extends BaseServlet {
    private BookService service =new BookServiceImpl();
    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1 获取请求的参数 pageNo 和 pageSize
        int pageNo = webUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = webUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        //2 调用BookService.page(pageNo，pageSize)：Page对象
        Page<Book> page = service.page(pageNo,pageSize);
        page.setUrl("client/clientBookServlet?action=page");
        //3 保存Page对象到Request域中
        req.setAttribute("page",page);
        //4 请求转发到pages/manager/book_manager.jsp页面
        req.getRequestDispatcher("/pages/client/index.jsp").forward(req,resp);
    }
    protected void pageByPrice(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       int min= webUtils.parseInt(req.getParameter("min"),0);
        int max = webUtils.parseInt(req.getParameter("max"), Integer.MAX_VALUE);
        int pageNo = webUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = webUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        Page<Book> page=service.pageByPrice(min,max,pageNo,pageSize);

        //设置点击下一页依然存在价格筛选后的结果
        StringBuilder sb = new StringBuilder("client/clientBookServlet?action=pageByPrice");
        //如果请求中有最大最小参数就追加到分页条的地址中
        if (req.getParameter("min")!=null){
            sb.append("&min=").append(req.getParameter("min"));
        }if (req.getParameter("max")!=null){
            sb.append("&max=").append(req.getParameter("max"));
        }
        page.setUrl(sb.toString());
        //3 保存Page对象到Request域中
        req.setAttribute("page",page);
        req.getRequestDispatcher("/pages/client/index.jsp").forward(req,resp);
    }
}
